package fx.game.demo.common;

import javafx.application.Application;
import javafx.application.HostServices;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.control.Hyperlink;
import javafx.scene.text.Font;

import java.util.HashMap;

/**
 *
 * @author: datom
 *
 * @create: 2021/12/6 23:20
 *
 * @Deprecated 超链接管理
 *
 **/
public class HyperlinkManage {

    /**
     * 应用类，用于打开超链接
     */
    private static Application application;


    /**
     *
     */
    private static HashMap<String, Hyperlink> hyperlinkHashMap;

    public static void setApplication(Application app) {
        application = app;
    }

    /**
     * @Author: datom
     * @Date: 2021/12/6 23:21
     *
     * @Description: 获取超链接
     *
     */
    public static Hyperlink getHyperlink(String text, String url) {
        if(hyperlinkHashMap == null) {
            hyperlinkHashMap = new HashMap<>();
        }

        if(application == null) {
            return null;
        }

        Hyperlink hyperlink = hyperlinkHashMap.get(text);
        if(hyperlink == null) {
            hyperlink = new Hyperlink(text);
            hyperlink.setFont(Font.font(18));
            hyperlink.setOnAction(new EventHandler<ActionEvent>() {
                @Override
                public void handle(ActionEvent event) {
                    application.getHostServices().showDocument(url);
                }
            });
            hyperlinkHashMap.put(text, hyperlink);
        }
        return hyperlink;
    }
}
